// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    username: '',
    password: '',
    checked: false,
  },

  //微信绑定之后的登录
  bindClick: function () {
    wx.login({
      success: (res) => {
        var login2bind = wx.request({
          url: 'https://127.0.0.1:10270/api/Login2WeiXin?code='+res.code,
          method: 'get',
          success: (result) => {
            if (result.data != null && result.data != -1 && !isNaN(result.data)) {
              getApp().globalData.userId = result.data
              wx.setStorageSync('userid', result.data)
              wx.navigateTo({
                url: '../mid/mid',
              })
            }else{
              wx.showToast({
                title: '登录失败，用户名或密码错误',
              })
            }
            login2bind.abort()
          },
        })
      }
    })
  },
  //使用账号密码登录
  logtoUserAndPass: function (data) {
    // var that = this 
    // wx.navigateTo({ 
    //   url: '../mid/mid', 
    // })
    var user2nick = data.detail.value.loginInput2name
    var pass = data.detail.value.pwd2log
    var passlist = pass.split("")
    var CharSelect = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    var pwd2log = ""
    for (let index = 0; index < pass.length; index++) {
      var charindexof = CharSelect.indexOf(passlist[index])+11;
      if (charindexof > CharSelect.length) {
        charindexof = charindexof - CharSelect.length - 1
      }
      const element = CharSelect.charAt(charindexof);
        pwd2log +=  element
    }
    this.logcheck();
    var pageReset = wx.request({
      url: 'https://127.0.0.1:10270/api/bindLogin?username='+user2nick+"&password="+pwd2log, 
      method: 'post',
      header:{
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: (result) => {
        // console.log(result);
        // console.log(isNaN(result.data));
        // console.log(result.data != -1);
        if (result.data != -1 && !isNaN(result.data)) {
          app.globalData.userId = result.data
          wx.setStorageSync('userid', result.data)
          wx.navigateTo({
            url: '../mid/mid',
          })
          pageReset.abort()
        }else{
          wx.showToast({
            title: '登录失败，用户名或密码错误',
          })
          pageReset.abort()
        }
      }
    })
  },
  //首次登录使用账号密码与微信绑定
  bindToUser: function () {
    // wx.redirectTo({
    //   url: "/pages/mid/mid",
    // })
     var user2nick = this.data.username 
     var pass = this.data.password
    var passlist = pass.split("")
    var CharSelect = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    var pwd2log = ""
    for (let index = 0; index < pass.length; index++) {
      var charindexof = CharSelect.indexOf(passlist[index])+11;
      if (charindexof > CharSelect.length) {
        charindexof = charindexof - CharSelect.length - 1
      }
      const element = CharSelect.charAt(charindexof);
        pwd2log +=  element
    }
    this.logcheck()
    wx.login({
      success: (res) => {
        // console.log(pwd2log);
        var req = wx.request({
          url: 'https://127.0.0.1:10270/api/Login2WeiXinFirst?username='
            + user2nick + '&password=' + pwd2log + '&code=' + res.code,
          method: 'post',
          success: (result) => {
            if (result.data != '登录失败' && result.data != null && result.data != -1 && !isNaN(result.data)) {
              // console.log(result.data);
              getApp().globalData.userId = result.data
              wx.setStorageSync('userid', result.data)
              this.logcheck()
              wx.navigateTo({
                url: "/pages/mid/mid",
              })
            }else{
              wx.showToast({
                title: '登录失败，用户名或密码错误',
              })
            }
            req.abort()
          },
        })
      },
    })
  },
logcheck:function () {
  var check = this.data.checked
    wx.setStorageSync('checked', check)
},
 checkChange: function (params) {
    // console.log(params.detail.value.length);
    if (params.detail.value.length == 1) {
      this.setData({
        checked: true
      })
    } else {
      this.setData({
        checked: false
      })
    }
  },
  replace2username: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
  replace2password: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  logintoTest:function (params) {
        wx.navigateTo({ 
      url: '../mid/mid', 
    }) 
  },
  onLoad() {
    var userid = wx.getStorageSync('userid')
    if (userid != -1 && userid != null) {
      getApp().globalData.userId = userid
      //console.log(app.globalData.userId);
    }
    var checked = wx.getStorageSync('checked')
    if (checked) {
      wx.navigateTo({
        url: '../mid/mid', 
      })
    }
  },
})
